using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NeosSDI.Core.MVC;
using AttributeRouting.Web.Mvc;
using Microsoft.Practices.Unity;
using NeosSDI.Transverse.Interfaces.Services;
using NeosSDI.MVC4.Web.Models;
using NeosSDI.Transverse.Interfaces.DAL;

namespace NeosSDI.MVC4.Web.Controllers
{
    /// <summary>
    /// Home controller
    /// </summary>
    public partial class HomeController : BaseController
    {
        /// <summary>
        /// Instance of a sample service
        /// </summary>
        [Dependency]
        public ISampleService SampleService { get; set; }


        /// <summary>
        /// Index page of the website
        /// </summary>
        /// <returns>Index view</returns>
        [GET("Index")]
        public virtual ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            return View();
        }

        /// <summary>
        /// Database creation
        /// </summary>
        /// <returns>Index view</returns>
        [GET("Database")]
        public virtual ActionResult Database()
        {
            ViewBag.Message = "Generate a sample database.";
            bool isDatabaseExist = false;
            try
            {
                isDatabaseExist = SampleService.CheckDatabaseExist();
            }
            catch (Exception)
            {
            }

            ViewBag.IsDatabaseExist = isDatabaseExist;

            return View();
        }

        /// <summary>
        /// Create database
        /// </summary>
        /// <returns>Index view</returns>
        [POST("DatabaseCreation")]
        public virtual ActionResult DatabaseCreation()
        {
            bool isDatabaseExist = false;
            try
            {
                isDatabaseExist = SampleService.CheckDatabaseExist();
            }
            catch (Exception)
            {
            }
            if (!isDatabaseExist)
            {
                System.IO.StreamReader _file = new System.IO.StreamReader(Server.MapPath("~/") + "//App_Data//Database creation script.sql");
                string script = _file.ReadToEnd();
                _file.Close();

                SampleService.CreateDatabase(script);
                return Content("Database have been created. Refresh page in order to execute some query.");
            }
            return Content("Database already exists. Refresh page in order to execute some query.");
        }

        /// <summary>
        /// Index page of the website
        /// </summary>
        /// <returns>Index view</returns>
        [POST("DatabaseQuery")]
        public virtual ActionResult DatabaseQuery()
        {
            ViewBag.Message = "Database.";
            DatabaseQueryModel databaseQueryModel = new DatabaseQueryModel();

            databaseQueryModel.Samples = SampleService.GetLastFiveSamples();

            var usersDto = SampleService.GetAllUser();
            List<User> user = new List<User>();
            databaseQueryModel.Users = AutoMapper.Mapper.Map(usersDto, user);

            return PartialView(NeosMVC.Home.Views.DatabaseQuery, databaseQueryModel);
        }

        /// <summary>
        /// About page
        /// </summary>
        /// <returns>About view</returns>
        [GET("About")]
        public virtual ActionResult About()
        {
            ViewBag.Message = "Your app description page.";
            return View();
        }

    }
}
